class Box2DTest
{
	private world:Box2D.Dynamics.b2World;
	private debug:Box2D.Dynamics.b2DebugDraw;
	private p2m:number = 30;

	private sWidth:number;
	private sHeight:number;

	private _curTest:Test;
	public constructor() 
	{
		super();
		//var t:Test = new Test();
	}

	private onAdded(e:egret.Event):void
	{
		this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAdded,this);

		 this._curTest= new Test(this);
		//this._curTest= new TestOneSidePlatform(this);
		// this._curTest= new TestBreakable(this);
		// this._curTest= new TestRagdoll(this);
		// this._curTest= new TestBuoyancy(this);
		// this._curTest= new TestStack(this);
		// this._curTest= new TestCrankGearsPulley(this);
		// this._curTest= new TestBridge(this);
		// this._curTest= new TestCCD(this);
		 //this._curTest= new TestCompound(this);
		// this._curTest= new TestEdges(this);
		// this._curTest= new TestRaycast(this);
		//this._curTest= new TestTheoJansen(this);
		/*
		this.sWidth = this.stage.stageWidth;
		this.sHeight = this.stage.stageHeight;

		this.createWorld();
		this.createDebug();

		this.createBox(this.sWidth/2,this.sHeight,this.sWidth,10,true);
        this.createBox(this.sWidth/2,0,this.sWidth,10,true);
        this.createBox(0,this.sHeight/2,10,this.sHeight,true);
        this.createBox(this.sWidth,this.sHeight/2,10,this.sHeight,true);

		for(var i:number = 0;i<20;i++)
		{
            this.createBox(Math.random()*300+50,
						   Math.random()*200+50,
						   Math.random()*30+10,
						   Math.random()*30+10);
        }
		*/
		this.addEventListener(egret.Event.ENTER_FRAME, this.onTick, this);
		
	}

	private onTick(e:egret.Event):void
	{
		this._curTest.update();
		//console.log("a" + this.world)
		// this.world.Step(1/60,10,10);
        // this.world.DrawDebugData();
	}
	private createWorld():void
	{
		var gravity:Box2D.Common.Math.b2Vec2 = new Box2D.Common.Math.b2Vec2(0,10);
		this.world = new Box2D.Dynamics.b2World(gravity,true);
		console.log(this.world);
	}

	private createDebug():void
	{
		this.debug = new Box2D.Dynamics.b2DebugDraw();
		this.debug.SetSprite(this);
		this.debug.SetDrawScale(30);
		this.debug.SetLineThickness(1);
		this.debug.SetAlpha(0.8);
		this.debug.SetFillAlpha(0.5);
		this.debug.SetFlags(Box2D.Dynamics.b2DebugDraw.e_shapeBit);
		this.world.SetDebugDraw(this.debug);
	}

	private createBox(posX:number,posY:number,w:number,h:number,isStatic:boolean=false){
        var bodyDef:Box2D.Dynamics.b2BodyDef = new Box2D.Dynamics.b2BodyDef();
        bodyDef.position = new Box2D.Common.Math.b2Vec2(posX/this.p2m,posY/this.p2m);
        bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
        if(isStatic) {
            bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
        }
        var body:Box2D.Dynamics.b2Body = this.world.CreateBody(bodyDef);
 
        var poly:Box2D.Collision.Shapes.b2PolygonShape;
        poly = Box2D.Collision.Shapes.b2PolygonShape.AsBox(w/this.p2m,h/this.p2m);
        var fixtureDef:Box2D.Dynamics.b2FixtureDef = new Box2D.Dynamics.b2FixtureDef();
        fixtureDef.density = 3;
        fixtureDef.restitution = 0.2;
        fixtureDef.shape = poly;
 
        body.CreateFixture(fixtureDef);
    }

}